/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "itempositioncalculator.h"

ItemPositionCalculator::ItemPositionCalculator()
{

}

ItemPositionCalculator::ItemPostion ItemPositionCalculator::calculationPosition(int desktopPageRow,
                                                                                int desktopPageColumn,
                                                                                ItemIdsInAllPageGrid &itemIdsInAllPageGrid,
                                                                                int rowSpan, int columnSpan)
{
    if (rowSpan <=0 || columnSpan <= 0 ||
            rowSpan > desktopPageRow || columnSpan > desktopPageColumn) {
        return std::make_tuple(-1, -1, -1);
    }

    bool newPageCreated = false;
    for (int page = 0; !newPageCreated; ++page) {
        if (!itemIdsInAllPageGrid.contains(page)) {
            itemIdsInAllPageGrid[page] = createGridItemIds(desktopPageRow, desktopPageColumn);
            newPageCreated = true;
        }
        auto itemsIds = itemIdsInAllPageGrid.value(page);
        int row, column;
        std::tie(row, column) = doCalculate(itemsIds, desktopPageRow, desktopPageColumn);
        if (row != -1) {
            return std::make_tuple(page, row, column);
        }
    }
    return std::make_tuple(-1, -1, -1);
}

ItemPositionCalculator::ItemIdsInPageGrid ItemPositionCalculator::createGridItemIds(int rowCount, int colCount)
{
    QList<QList<int> > itemIdsGrid;
    for (int row=0; row < rowCount; ++row) {
        QList<int> columnItemIds;
        for (int col=0; col < colCount; ++col) {
            columnItemIds.append(-1);
        }
        itemIdsGrid.append(columnItemIds);
    }
    return itemIdsGrid;
}

AppWidgetPositionCalculator::AppWidgetPositionCalculator()
{

}

ItemPositionCalculator::ItemPostionInOnePage AppWidgetPositionCalculator::doCalculate(const ItemIdsInPageGrid &itemIdsInPageGrid,
                                                                                      int desktopPageRow, int desktopColumnSpan,
                                                                                      int rowSpan, int columnSpan)
{
    for (int col=0; col < desktopColumnSpan; ++col) {
        for (int row=0; row < desktopPageRow; ++row) {
            if (itemIdsInPageGrid[row][col] > 0) {
                continue;
            }
            int endRow = row + rowSpan - 1;
            int endCol = col + columnSpan - 1;
            if (endRow < desktopPageRow && endCol < desktopColumnSpan) {
                return std::make_tuple(row, col);
            }
        }
    }
    return std::make_tuple(-1, -1);
}

ItemPositionCalculator::ItemPostionInOnePage AppItemPositionCalculator::doCalculate(const ItemIdsInPageGrid &itemIdsInPageGrid,
                                                                                    int desktopPageRow, int dekstopPageColumn,
                                                                                    int rowSpan, int columnSpan)
{
    for (int row=0; row < desktopPageRow; ++row) {
        for (int col=0; col < dekstopPageColumn; ++col) {
            if (itemIdsInPageGrid[row][col] > 0) {
                continue;
            }
            int endRow = row + rowSpan - 1;
            int endCol = col + columnSpan - 1;
            if (endRow < desktopPageRow && endCol < dekstopPageColumn) {
                return std::make_tuple(row, col);
            }
        }
    }
    return std::make_tuple(-1, -1);
}
